<template>
  <div>
    <div class="contBtn_left">
      <v-btn
        :btnConfig="btnConfig"
        @confirm="confirm"
        @comeback="comeback"
        @cleanup="cleanup"
      ></v-btn>
    </div>
    <div class="contBtn_right"></div>
  </div>
</template>

<script>
import btn from "@/components/commons/TableTitle";

export default {
  name: "",
  props: {},
  data() {
    return {
      btnConfig: [
        { label: this.$t('common.confirm'), invalid: "confirm" },
        { label: this.$t('common.return'), invalid: "comeback" },
        { label: this.$t('common.clear'), invalid: "cleanup" },
      ],
    };
  },
  methods: {
    async confirm() {
      let inputData = this.$parent.$refs.input.inputData;
      let input = this.$parent.$refs.input.$refs.input;
      let receiptlpn = this.$trim(inputData.receiptlpn).toUpperCase();
      let receiptkey = inputData.receiptkey;
      if (receiptlpn === "") {
        this.$message.error(this.$inputPlease("in.standardlpn"));
        input.inputFocus("receiptlpn");
        this.errorPlay();
        return;
      }
      try {
        let res = await this.$api.post(
          `/ReceiptReversalConfirm?r=${new Date().getTime()}`,
          {
            whseid: this.$parent.whseid,
            opeuser: this.$parent.opeuser,
            users: this.$parent.users,
            receiptlpn: receiptlpn,
            receiptkey: receiptkey,
          }
        );
        if (res.data.type === "error") {
          this.$message.error(res.data.msg);
          this.errorPlay();
        } else if (res.data.type === "success") {
          this.$message.success(this.$t('msg.msg79'));
          this.this.$parent.$refs.input.inputData = {
            receiptkey: receiptkey,
            suppliername: "",
            receiptlpn: "",
            sku: "",
            qty: "",
            totalqty: "",
          };
          input.inputFocus("receiptlpn");
        }
      } catch (e) {
        this.$message.error(this.$t('app.err'));
        this.errorPlay();
      }
    },
    async comeback() {
      let inputData = this.$parent.$refs.input.inputData;
      let input = this.$parent.$refs.input.$refs.input;
      let receiptlpn = this.$trim(inputData.receiptlpn).toUpperCase();
      let receiptkey = inputData.receiptkey;
      let storerkey = this.$parent.storerkey;
      let sku = inputData.sku;
      let qty = inputData.qty;
      if (receiptlpn === "") {
        this.$message.error(this.$inputPlease("in.standardlpn"));
        input.inputFocus("receiptlpn");
        this.errorPlay();
        return;
      }
      if (sku === "") {
        this.$message.error(this.$inputPlease("config.sku"));
        input.inputFocus("sku");
        this.errorPlay();
        return;
      }
      if (qty === 0) {
        this.$message.error(this.$t('msg.msg66'));
        this.errorPlay();
        return;
      }
      try {
        let res = await this.$api.post(
          `/ReceiptReversalBack?r=${new Date().getTime()}`,
          {
            whseid: this.$parent.whseid,
            opeuser: this.$parent.opeuser,
            users: this.$parent.users,
            receiptlpn: receiptlpn,
            receiptkey: receiptkey,
            storerkey: storerkey,
            sku: sku,
          }
        );
        if (res.data.type === "error") {
          this.$message.error(res.data.msg);
          this.errorPlay();
        } else if (res.data.type === "success") {
          this.$parent.$refs.input.inputData.qty = res.data.qty;
          this.$parent.$refs.input.inputData.totalqty = res.data.totalqty;
          this.$parent.$refs.table.tableData = res.data.jsonList;
          this.$parent.$refs.input.inputData.sku = "";
          input.inputFocus("sku");
        }
      } catch (e) {
        this.$message.error(this.$t('app.err'));
        this.errorPlay();
      }
    },
    async cleanup() {
      let receiptkey = this.$parent.$refs.input.inputData.receiptkey;
      if (receiptkey === "") {
        this.$message.error(this.$tj('in.asnCode','app.emptyMes'));
        this.errorPlay();
        return;
      }
      try {
        let res = await this.$api.post(`/ReceiptReversalCleanUp`, {
          whseid: this.$parent.whseid,
          users: this.$parent.users,
          receiptkey: receiptkey,
        });
        if (res.data.type === "error") {
          this.$message.error(res.data.msg);
          this.errorPlay();
        } else if (res.data.type === "success") {
          this.$message.success(this.$t('msg.msg79'));
          this.this.$parent.$refs.input.inputData = {
            receiptkey: receiptkey,
            suppliername: "",
            receiptlpn: "",
            sku: "",
            qty: "",
            totalqty: "",
          };
        }
      } catch (e) {
        this.$message.error(this.$t('app.err'));
        this.errorPlay();
      }
    },
  },
  mounted() {},
  components: {
    "v-btn": btn,
  },
};
</script>

<style scoped></style>
